<?php
/**
 * 管理员操作模型
 * Enter description here ...
 * @author Code.404
 *
 */
import('ORG.Util.Input');
import('@.ORG.FAC');
class AdminModel extends Model{

	protected $_validate = array(
		array('username','require','ACCOUNT_HAS_EXISTS',MODEL::EXISTS_VAILIDATE ,'unique',1),
		array('password','require','PASSWORD_MUST',MODEL::EXISTS_VAILIDATE )
	);
	
	private $cache_data = null;

	/**
	 * 添加新用户
	 * Enter description here ...
	 * @param unknown_type $user
	 */
	function addUser($user){
		$_data['username'] = FAC::F($user['username'],FAC::USERNAME);
		$_data['password'] = FAC::F($user['password'],FAC::PASSWORD);
		$_data['create_time'] = $_data['update_time'] = time();

		if($this->create($_data)){
			if($this->add()){
				return true;
			}
		}
		return false;
	}
	/**
	 * 修改密码
	 * Enter description here ...
	 * @param unknown_type $uid
	 * @param unknown_type $new_password
	 */
	function changePassword($uid , $new_password){
		$map['id'] = FAC::F($uid , FAC::INT);
		$password = FAC::F($new_password , FAC::PASSWORD);
		
		return $this->where($map)->setField('password' , $password);
	}
	
	/**
	 * 验证用户名和密码是否正确
	 * Enter description here ...
	 * @param unknown_type $username
	 * @param unknown_type $password
	 */
	function verifyUser($username , $password){
		
		$verify['username'] = FAC::F($username,FAC::USERNAME);
		$verify['password'] = FAC::F($password,FAC::PASSWORD);
		
		$this->setData($this->where($verify)->find());
		if($this->getData()){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 设置查询到的用户数据
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	private function setData($data){
		$this->cache_data = null;
		$this->cache_data = $data;
	}
	/**
	 * 用于获取查询到的用户数据
	 * Enter description here ...
	 */
	public function getData(){
		return $this->cache_data;
	}
}
